/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/13
 * Time: 14:02
 */
public class Solution {
//    public int minSubArrayLen(int target, int[] nums) {
//        int left = 0;
//        int right = 0;
//        int sum = 0;
//        int length = Integer.MAX_VALUE;
//        while(right < nums.length){
//            sum += nums[right];
//            right++;
//            while(sum >= target){
//                sum -= nums[left];
//                left++;
//                length =  Math.min(length, right - left + 1);
//            }
//        }
//        if(left == 0 && right == nums.length) length = 0;
//        return length;
//    }
    public int minSubArrayLen(int target, int[] nums){
        int sum = 0, length = Integer.MAX_VALUE;
        for(int left = 0,right = 0; right < nums.length; right++){
            sum += nums[right];
            while(sum >= target){
                length = Math.min(length, right - left + 1);
                sum -= nums[left++];
            }
        }
        return length == Integer.MAX_VALUE ? 0 : length;
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        int ret = s.minSubArrayLen(15,new int[]{5,1,3,5,10,7,4,9,2,8});
        System.out.println(ret);
    }
}